<script lang="ts">
export default {
  name: "EntityDialog"
}
</script>

<template>
  <!-- 对话框 -->
  <el-dialog :close-on-click-modal="false" :model-value="visible" :title="dataState.title" min-width="200px"
             @close="handleCancel">

    <!-- 数据表单 -->
    <el-form ref="dataFormRef" :model="dataState.formData" :rules="dataState.rules" label-width="100px">

      <!-- 编辑框：分组名称 -->
      <el-form-item label="名称" prop="groupName">
        <el-input v-model="dataState.formData.groupName" :disabled="dataState.type==='update'"/>
      </el-form-item>

      <el-form-item label="描述" prop="description">
        <el-input v-model="dataState.formData.description" :disabled="dataState.type==='update'"/>
      </el-form-item>

    </el-form>

    <!-- 脚部：确认/取消按钮-->
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </template>

  </el-dialog>
</template>

<script lang="ts" setup>
import {onMounted, reactive, ref} from "vue";

import {ElForm} from "element-plus";
import {CreateRepoGroupRequestVO} from "@/api/cloud/repository/repogroup/types";

const props = defineProps(['visible']);
const emit = defineEmits(['create', 'update', 'cancel']); //注意：这个是父组件<dict-type-edit>标签上的@close
const dataFormRef = ref(ElForm); // 属性名必须和元素的ref属性值一致

// 数据状态：定义
let dataState = reactive({
  // 类型：create/update
  type: 'create',
  // 标题
  title: '添加属组',
  // 表单数据
  formData: {
    groupName: '',
    description: '',
  } as CreateRepoGroupRequestVO,
  // 对话框的各控件输入规则
  rules: {
    groupName: [{required: true, message: '必填項', trigger: 'blur'}],
  },
});

/**
 * 取消
 */
function handleCancel() {
  //执行调用<config-edit-dialog @cancel="cancelEdit">
  emit('cancel', "子组件传给父组件的值");
}

/**
 * 确认：新增/修改实体
 */
function handleSubmit() {
  if (dataState.type === 'create') {
    // 执行调用<config-edit-dialog @create="createEdit">的create
    emit('create', dataState.formData);
  }
  if (dataState.type === 'update') {
    //执行调用<config-edit-dialog @update="updateEdit">
    emit('update', dataState.formData);
  }
}


/**
 * 响应页面安装：页面的初始化工作
 */
onMounted(() => {
});

</script>
